<?php
namespace model;
require_once ('src/Model/dbModel.php');

class UserModel {
    private $m_hasCorrectUsername = false;
    private $m_hasCorrectPassword = false;
    private $m_correctUsername = "";
    private $m_correctPassword = "";
    private $dbModel;


    /**
    * __constructor
    */
    public function __construct()     {
        $this->dbModel = new \model\dbModel();
    }


    /**
    * @param string $userUsername
    * @param string $userPassword
    * @return bolean
    *
    * Här kontrollerar vi användaruppgifterna om dom finns i databasen eller inte
    */
	public function checkUserCredentials($userUsername, $userPassword) {
        $this->dbModel->connect();
        $result = mysql_query("SELECT * FROM users WHERE username LIKE '". $userUsername."'");
        $this->dbModel->close();
        $num_rows = mysql_num_rows($result);

        if ( $num_rows > 0) {
            $username ="";
            $password ="";

            while($row = mysql_fetch_array($result)) {
                $username =$row[1];
                $password =$row[2];
            }

            if($userUsername == $username) {
                $this->m_hasCorrectUsername = true;

                if($userPassword == $password) {
                    $this->m_hasCorrectPassword = true;
                    $this->m_correctUsername = $userUsername;
                    $this->m_correctPassword = $userPassword;
                }
            }
        }

        if ($this->m_hasCorrectUsername == true && $this->m_hasCorrectPassword == true) {
           return true;
        } else {
            return false;
        }
	}


    /**
    * @param string $userName
    * @param string $password
    *
    * här läggs en ny användare till i databasen
    */
    public function addUserToDatabase($userName , $password) {
        $this->dbModel->connect();
        mysql_query("INSERT INTO users (username, password) VALUES ('". $userName ."', '". $password. "')");
        $this->dbModel->close();
    }


    /**
    * @return array $result
    *
    * här hämtar vi ut alla registrerade användare från databasen
    */
    public function returnAllUsersInDatabase() {
        $this->dbModel->connect();
        $result = mysql_query("SELECT * FROM users");
        $this->dbModel->close();
        return $result;
    }


    /**
    * @param string $userId
    *
    * här tas en användare bort från databasen
    */
    public function removeUserFromDatabase($userId) {
        $this->dbModel->connect();
        $result = mysql_query("SELECT username FROM users WHERE id LIKE '". $userId."'");
        while($row = mysql_fetch_array($result)) {
                $username =$row[0];
        }
        mysql_query("DELETE FROM comments WHERE user='".$username."'");
        mysql_query("DELETE FROM blogposts WHERE user='".$username."'");
        mysql_query("DELETE FROM users WHERE id='".$userId."'");
        $this->dbModel->close();
    }


    /**
    * @param string $userId
    *
    * här sätts att en användare ska ha admin-behörighet
    */
    public function setAdminUserInDatabase($userId) {
        $this->dbModel->connect();
        mysql_query("UPDATE users SET admin_user=1 WHERE id ='$userId'");
        $this->dbModel->close();
    }


    /**
    * @param string $userUsername
    * @return boolean
    *
    * här kollas om användarnamnet är upptaget eller ej
    */
    public function isUserNameTaken($userUsername) {
        $this->dbModel->connect();
        $result = mysql_query("SELECT * FROM users WHERE username LIKE '". $userUsername."'");
        $this->dbModel->close();
        $num_rows = mysql_num_rows($result);

        if ( $num_rows > 0) {
            return true;
            } else {
                return false;
        }
    }


    /**
    * @return boolean
    *
    * här kollas om inloggad användare har admin-behörighet
    */
    public function isUserAdmin() {
        $username = $this->m_correctUsername;
        $this->dbModel->connect();
        $result = mysql_query("SELECT * FROM users WHERE username LIKE '". $username."'");
        $this->dbModel->close();

        while($row = mysql_fetch_array($result)) {
            $isUserAdmin =$row[3];
        }

        if ( $isUserAdmin == 1) {
            return true;
            } else {
                return false;
        }
    }


    /**
    * @return boolean
    *
    * här kollas om användaren matat in ett korrekt användarnamn
    */
    public function hasValidUsername() {
        return $this->m_hasCorrectUsername;
    }


    /**
    * @return boolean
    *
    * här kollas om användaren matat in ett korrekt lösenord
    */
    public function hasValidPassword() {
        return $this->m_hasCorrectPassword;
    }


    /**
    * @return string
    *
    * här returneras det inmatade användarnamnet
    */
    public function returnValidUsername() {
        return $this->m_correctUsername;
    }


    /**
    * @return string
    *
    * här returneras det inmatade lösenordet
    */
    public function returnValidPassword() {
        return $this->m_correctPassword;
    }


}

